package org.xbill.DNS.spi;

import [B;
import java.io.PrintStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import org.xbill.DNS.AAAARecord;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.ExtendedResolver;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Name;
import org.xbill.DNS.PTRRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.ReverseMap;
import org.xbill.DNS.TextParseException;

public class DNSJavaNameService
  implements InvocationHandler
{
  private static final String domainProperty = "sun.net.spi.nameservice.domain";
  private static final String nsProperty = "sun.net.spi.nameservice.nameservers";
  private static final String v6Property = "java.net.preferIPv6Addresses";
  private boolean preferV6 = false;

  protected DNSJavaNameService()
  {
    String str1 = System.getProperty("sun.net.spi.nameservice.nameservers");
    String str2 = System.getProperty("sun.net.spi.nameservice.domain");
    String str3 = System.getProperty("java.net.preferIPv6Addresses");
    if (str1 != null)
    {
      localStringTokenizer = new StringTokenizer(str1, ",");
      arrayOfString1 = new String[localStringTokenizer.countTokens()];
      i = 0;
      if (localStringTokenizer.hasMoreTokens())
        break label115;
    }
    try
    {
      Lookup.setDefaultResolver(new ExtendedResolver(arrayOfString1));
      label115: if (str2 != null);
    }
    catch (UnknownHostException localUnknownHostException)
    {
      try
      {
        while (true)
        {
          String[] arrayOfString2 = new String[1];
          arrayOfString2[0] = str2;
          Lookup.setDefaultSearchPath(arrayOfString2);
          if ((str3 != null) && (str3.equalsIgnoreCase("true")))
            this.preferV6 = true;
          return;
          int j = i + 1;
          arrayOfString1[i] = localStringTokenizer.nextToken();
          i = j;
        }
        localUnknownHostException = localUnknownHostException;
        System.err.println("DNSJavaNameService: invalid sun.net.spi.nameservice.nameservers");
      }
      catch (TextParseException localTextParseException)
      {
        while (true)
          System.err.println("DNSJavaNameService: invalid sun.net.spi.nameservice.domain");
      }
    }
  }

  public String getHostByAddr(byte[] paramArrayOfByte)
    throws UnknownHostException
  {
    Record[] arrayOfRecord = new Lookup(ReverseMap.fromAddress(InetAddress.getByAddress(paramArrayOfByte)), 12).run();
    if (arrayOfRecord == null)
      throw new UnknownHostException();
    return ((PTRRecord)arrayOfRecord[0]).getTarget().toString();
  }

  public Object invoke(Object paramObject, Method paramMethod, Object[] paramArrayOfObject)
    throws Throwable
  {
    int i;
    [B[] arrayOf[B;
    int j;
    Object localObject;
    try
    {
      if (paramMethod.getName().equals("getHostByAddr"))
      {
        localObject = getHostByAddr((byte[])paramArrayOfObject[0]);
        break label149:
      }
      if (paramMethod.getName().equals("lookupAllHostAddr"))
      {
        InetAddress[] arrayOfInetAddress = lookupAllHostAddr((String)paramArrayOfObject[0]);
        Class localClass = paramMethod.getReturnType();
        if (localClass.equals([Ljava.net.InetAddress.class))
        {
          localObject = arrayOfInetAddress;
          break label149:
        }
        if (localClass.equals([[B.class))
        {
          i = arrayOfInetAddress.length;
          arrayOf[B = new byte[i][];
          j = 0;
          break label152:
          arrayOf[B[j] = arrayOfInetAddress[j].getAddress();
          ++j;
        }
      }
    }
    catch (Throwable localThrowable)
    {
      System.err.println("DNSJavaNameService: Unexpected error.");
      localThrowable.printStackTrace();
      throw localThrowable;
    }
    throw new IllegalArgumentException("Unknown function name or arguments.");
    while (true)
    {
      label149: label152: 
      do
        return localObject;
      while (j < i);
      localObject = arrayOf[B;
    }
  }

  public InetAddress[] lookupAllHostAddr(String paramString)
    throws UnknownHostException
  {
    Name localName;
    Record[] arrayOfRecord;
    try
    {
      localName = new Name(paramString);
      arrayOfRecord = (Record[])null;
      if (this.preferV6)
        arrayOfRecord = new Lookup(localName, 28).run();
      if (arrayOfRecord == null)
        arrayOfRecord = new Lookup(localName, 1).run();
      if ((arrayOfRecord == null) && (!(this.preferV6)))
        arrayOfRecord = new Lookup(localName, 28).run();
      if (arrayOfRecord == null)
        throw new UnknownHostException(paramString);
    }
    catch (TextParseException localTextParseException)
    {
      throw new UnknownHostException(paramString);
    }
    InetAddress[] arrayOfInetAddress = new InetAddress[arrayOfRecord.length];
    int i = 0;
    if (i >= arrayOfRecord.length)
      return arrayOfInetAddress;
    arrayOfRecord[i];
    if (arrayOfRecord[i] instanceof ARecord)
      arrayOfInetAddress[i] = ((ARecord)arrayOfRecord[i]).getAddress();
    while (true)
    {
      while (true)
        ++i;
      arrayOfInetAddress[i] = ((AAAARecord)arrayOfRecord[i]).getAddress();
    }
  }
}